package game.module.user.processor;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.entity.PlayingRole;
import game.session.SessionManager;
import lion.common.MsgCodeAnn;
import lion.netty4.message.GamePlayer;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lion.netty4.processor.MsgProcessor;
import lua.LuaMessageHall.C2SServerReconnect;
import lua.LuaMessageHall.S2CServerReconnect;

@MsgCodeAnn(msgcode = C2SServerReconnect.id, accessLimit = 500)
public class HallReconnectProcessor extends MsgProcessor {

	private static Logger logger = LoggerFactory.getLogger(HallReconnectProcessor.class);

	@Override
	public void process(GamePlayer player, RequestMessageRaw request) throws Exception {
		C2SServerReconnect downlineReconnectMsg = C2SServerReconnect.parse(request);
		Long inGameSessionId = downlineReconnectMsg.session_id;
		logger.info("1109-DownlineReconnect,inGameSessionId={},addr={}", inGameSessionId, player.getAddress());
		PlayingRole playingRole = SessionManager.getInstance().getPlayer(inGameSessionId);
		// session id 无效
		if (playingRole == null) {
			logger.info("player not exist!addr={}", player.getAddress());
			S2CServerReconnect retMsg = new S2CServerReconnect(1,2);
			player.writeAndFlush(retMsg.build(player.alloc()));
			return;
		}
		GamePlayer oldGamePlayer = playingRole.getGamePlayer();
		if (oldGamePlayer != null) {
//			if (oldGamePlayer == player || inGameSessionId.equals(oldGamePlayer.getSessionId()) && oldGamePlayer.isChannelActive()) {
			if (oldGamePlayer == player) {
				logger.info("channel is the same!addr={}", player.getAddress());
				SessionManager.getInstance().visit(inGameSessionId);
				S2CServerReconnect retMsg = new S2CServerReconnect(0,2);
				player.writeAndFlush(retMsg.build(player.alloc()));
				return;
			} else {
				oldGamePlayer.saveSessionId(0L);
				oldGamePlayer.close();
			}
		}
		// online
		SessionManager.getInstance().online(playingRole);
		playingRole.setGamePlayer(player);
		player.saveSessionId(inGameSessionId);
		SessionManager.getInstance().visit(inGameSessionId);
		// if (playingRole.getLevelGoTimeout() == null) {
		// ActivityPlayerManager.getInstance().levelGoTimeEndTimer(playingRole);
		// }
		// if (playingRole.getLoginTimeout() == null) {
		// ActivityPlayerManager.getInstance().loginTimeEndTimer(playingRole);
		// }
		playingRole.getPlayerCacheStatus().setEnterGameTime(new Date());
		// chat cache
		// int guildId = GuildCache.getInstance().getGuildId(playingRole.getId());
		// GuildChatCache.getInstance().addPlayer(guildId, playingRole);
		// ret
		S2CServerReconnect retMsg = new S2CServerReconnect(0,2);
		player.writeAndFlush(retMsg.build(player.alloc()));
	}

	@Override
	public void process(GamePlayer player, RequestProtoMessage request) throws Exception {

	}

	@Override
	public void process(GamePlayer player, MyRequestMessage request) throws Exception {

	}

}
